﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Common.Models.Base;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.CmsContents;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.Utilities;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.MusicManager
{
    public partial class CreateEditMusic : AdminPage
    {
        #region Khai báo
        private int musicManageId = 0;
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Directory.Exists(Server.MapPath("/Uploads/" + CurrentUser.UserID)))
                    Directory.CreateDirectory(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtImage.fpUploadDir = txtFileMusic.fpUploadDir = "/Uploads/" + CurrentUser.UserID;
                txtDescription.Height = 180;
                txtDescription.Width = 460;
                txtDescription.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                musicManageId = ObjConvertUtility.ToInt32(Request.QueryString["musicManageId"]);
                if (!Page.IsPostBack)
                {
                    DropMusician();
                    DropGroupMusic();
                    DropSinger();
                    CMSZonesBL.GetSub(dropZoneId, "0", 1, 0, CurrentUser.UserID);
                    LoadData();
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (musicManageId > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    MusicManagerInfo info = MusicManagerBL.Select(musicManageId);
                    if (info != null)
                    {
                        dropMusicianId.SelectedValue = info.MusicianId.ToString();
                        dropGroupMusicId.SelectedValue = info.GroupMusicId.ToString();
                        dropSingerId.SelectedValue = info.SingerId.ToString();
                        dropZoneId.SelectedValue = info.ZoneId.ToString();
                        txtMucsicName.Text = info.MucsicName;
                        txtPrice.Text = info.Price.ToString();
                        txtDescription.Text = info.Desciption;
                        txtKeyword.Text = info.Keyword;
                        txtImage.Text = info.Images;
                        txtFileMusic.Text = info.FileMusic;
                        dropShortCode.SelectedValue = info.ShortCode;
                        txtCodeId.Text = info.CodeId;
                        txtCreateOn.SelectedDate = Convert.ToDateTime(info.CreatedOn.ToString("MM/dd/yyyy"));
                        SelectedCheckBox(info);
                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                    txtCreateOn.SelectedDate =Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region Chỉnh sửa
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                MusicManagerInfo info = new MusicManagerInfo();
                FillData(info);
                MusicManagerBL.Update(info);               
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region Thêm mới
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                MusicManagerInfo info = new MusicManagerInfo();
                FillData(info);
                int i = MusicManagerBL.Insert(info);                
                if (i > 0)
                {                    
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region Load Data DropList
        void DropMusician()
        {
            dropMusicianId.DataBind();
            dropMusicianId.Items.Insert(0, new ListItem("Lựa chọn tên tác giả", "0"));
        }
        void DropSinger()
        {
            CHRTList<SingerInfo> list = SingerBL.SelectAll(Const.show);
            if (list != null && list.Count > 0)
            {
                dropSingerId.DataSource = list;
                dropSingerId.DataTextField = "SingerName";
                dropSingerId.DataValueField = "SingerId";
                dropSingerId.DataBind();
                dropSingerId.Items.Insert(0, new ListItem("Lựa chọn ca sỹ", "0"));
            }
        }
        void DropGroupMusic()
        {
            dropGroupMusicId.DataBind();
            dropGroupMusicId.Items.Insert(0, new ListItem("Lựa chọn nhóm thể hiện", "0"));
        }
        #endregion

        #region FillData
        void FillData(MusicManagerInfo info)
        {
            info.MusicManageId = musicManageId;
            info.MusicianId = int.Parse(dropMusicianId.SelectedValue);
            info.GroupMusicId = int.Parse(dropGroupMusicId.SelectedValue);
            info.SingerId = int.Parse(dropSingerId.SelectedValue);
            info.ZoneId = int.Parse(dropZoneId.SelectedValue);
            info.MucsicName = txtMucsicName.Text;
            info.Price = float.Parse(txtPrice.Text);
            info.Desciption = txtDescription.Text;
            info.PageViews = 0;
            info.NumberDownload = 0;
            info.Keyword = txtKeyword.Text;
            info.CreatedBy = CurrentUser.Username;
            info.CreatedOn = DateTime.Now;
            info.Type = 0;
            info.Lang = 1;
            info.Status = 2;
            info.FileMusic = txtFileMusic.Text;
            info.ShortCode = dropShortCode.SelectedValue;
            info.Type = CheckType();
            info.CodeId = txtCodeId.Text.Trim();
            info.CreatedOn = Convert.ToDateTime(txtCreateOn.SelectedDate.ToString("MM/dd/yyyy"));
            if (txtImage.Text.Trim() != info.Images && txtImage.Text.Trim().Length > 0)// Nếu thay đổi ảnh -> Thuc hien Crop anh
            {
                info.Images = ConvertImage.SetAvatarThumbnailWidthDirection(Server.MapPath(txtImage.Text.Trim()), 65, "_musician");
            }
            else
            {
                if (txtImage.Text.Length > 0)
                {
                    info.Images = txtImage.Text.Trim();
                }
                else
                {
                    info.Images = "";
                }
            }
        }
        #endregion

        #region ClearText
        void ClearText()
        {
            txtPrice.Text = "0";
            txtMucsicName.Text = "";
            txtImage.Text = "";
            txtDescription.Text = "";
            txtKeyword.Text = "";
            txtFileMusic.Text = "";
        }
        #endregion

        #region CheckType
        int CheckType()
        {
            CheckBox[] Boxes = new CheckBox[9];
            Boxes[0] = chboxNhacHot;
            Boxes[1] = chboxNhacMoiNhat;
            Boxes[2] = chboxViettel;
            Boxes[3] = chboxVinaphone;
            Boxes[4] = chboxMobifone;
            Boxes[5] = chboxVietnamobile;
            Boxes[6] = chboxEVNtelecom;
            Boxes[7] = chboxSfone;
            Boxes[8] = chboxBeeline;
            return ClsCheckBox.ReturnStatusCheck(Boxes);
        }
        #endregion

        #region SelectedCheckBox
        void SelectedCheckBox(MusicManagerInfo managerInfo)
        {
            CheckBox[] Boxes = new CheckBox[9];
            Boxes[0] = chboxNhacHot;
            Boxes[1] = chboxNhacMoiNhat;

            Boxes[2] = chboxViettel;
            Boxes[3] = chboxVinaphone;
            Boxes[4] = chboxMobifone;
            Boxes[5] = chboxVietnamobile;
            Boxes[6] = chboxEVNtelecom;
            Boxes[7] = chboxSfone;
            Boxes[8] = chboxBeeline;

            chboxNhacHot.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.nhacHotNhat, managerInfo.Type, Boxes);
            chboxNhacMoiNhat.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.nhacMoiNhat, managerInfo.Type, Boxes);

            chboxViettel.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.Viettel, managerInfo.Type, Boxes);
            chboxVinaphone.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.Vinaphone, managerInfo.Type, Boxes);
            chboxMobifone.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.Mobifone, managerInfo.Type, Boxes);
            chboxVietnamobile.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.Vietnamobile, managerInfo.Type, Boxes);
            chboxEVNtelecom.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.EVNtelecom, managerInfo.Type, Boxes);
            chboxSfone.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.Sfone, managerInfo.Type, Boxes);
            chboxBeeline.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.Beeline, managerInfo.Type, Boxes);

        }
        #endregion
    }
}